package com.scau.classOnline.service;

import com.scau.classOnline.model.PushCodeModel;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * @author LAN
 * @description 生成推流码
 * @date 2019/7/12
 */
@Service
public class PushLiveService{
    public Object getPushCode(String cid){
        String roomId = cid;
        String bizId = "55222";
        long time = getEndTime();
        String streamName = bizId+"_studyLive_"+roomId;
        String txtSecret = getSafeUrl("193d08b7b96595499e362f79f779babd",streamName,time);
        String pushCode = "rtmp://"+bizId+".livepush.myqcloud.com/live/"+streamName+"?"+txtSecret;
        PushCodeModel pcv = new PushCodeModel(pushCode);
        return pcv;
    }

    private long getEndTime(){
        String parm = "yyyy-MM-dd HH:mm:ss";
        Date current = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(current);
        calendar.add(Calendar.DATE, 1);
        Date endTime = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat(parm);
        String formattedTime = sdf.format(endTime);
        String tmpTime = null;
        try {
            tmpTime = String.valueOf(sdf.parse(formattedTime).getTime()/1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Long.parseLong(tmpTime);
    }

    private final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /*
     * KEY+ streamName + txTime
     */
    private String getSafeUrl(String key, String streamName, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamName).
                append(Long.toHexString(txTime).toUpperCase()).toString();

        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return txSecret == null ? "" :
                new StringBuilder().
                        append("txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }

    private String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}
